<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="108px"
    >
      <el-form-item label="文档标识" prop="contentId">
        <el-input
          v-model="queryParams.contentId"
          placeholder="请输入文档标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文档类型" prop="contentTypeId">
        <el-select
          v-model="queryParams.contentTypeId"
          placeholder="请选择文档类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in contentTypeIdOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="文档状态" prop="statusId">
        <el-select
          v-model="queryParams.statusId"
          placeholder="请选择文档状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in statusIdOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="文档名称" prop="contentName">
        <el-input
          v-model="queryParams.contentName"
          placeholder="请输入文档名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建日期">
        <el-date-picker
          v-model="daterangeCreatedDate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="创建人标识" prop="createdByUserLogin">
        <el-input
          v-model="queryParams.createdByUserLogin"
          placeholder="请输入创建人标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="版本号" prop="version">
        <el-select
          v-model="queryParams.version"
          placeholder="请选择版本号"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in versionOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['doc:content:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['doc:content:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['doc:content:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['doc:content:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-user"
          size="mini"
          :disabled="single"
          @click="handleStartWorkflow"
          v-hasPermi="['doc:content:start']"
          >提交审批</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="contentList"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文档标识" align="left" prop="contentId" />
      <el-table-column label="文档类型" align="left" prop="contentTypeId" :formatter="contentTypeIdFormat"/>
      <el-table-column
        label="文档状态"
        align="left"
        prop="statusId"
        :formatter="statusIdFormat"
      />
      <el-table-column label="文档名称" align="left" prop="contentName" width="280"/>
      <el-table-column
        label="创建日期"
        align="left"
        prop="createdDate"
        width="180"
      >
      </el-table-column>
      <el-table-column
        label="创建人"
        align="left"
        prop="createdByUserLogin"
      />
      <el-table-column
        label="上次修改日期"
        align="center"
        prop="lastModifiedDate"
        width="180"
      >
      </el-table-column>
      <el-table-column
        label="上次修改用户"
        align="left"
        prop="lastModifiedByUserLogin"
      />
      <el-table-column label="版本号" align="left" prop="version" width="80"/>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['doc:content:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['doc:content:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文档对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文档类型" prop="contentTypeId">
          <el-select v-model="form.contentTypeId" placeholder="请选择文档类型">
            <el-option
              v-for="dict in contentTypeIdOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="附件">
          <fileUpload
            v-model="form.objectInfo"
            @reveiveEvent="reveiveOriginName"
            :serverUrl  ="serverUrl"
          />
        </el-form-item>
        <el-form-item label="文档名称" prop="contentName">
          <el-input v-model="form.contentName" placeholder="请输入文档名称" />
        </el-form-item>
        <el-form-item label="文档描述">
          <el-input v-model="form.description" type="textarea" placeholder="请输入文档描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listContent,
  getContent,
  delContent,
  addContent,
  updateContent,
  exportContent,
  startWorkflowContent,
} from "@/api/doc/content";
import FileUpload from "@/components/FileUpload";
import Editor from "@/components/Editor";

export default {
  name: "Content",
  components: {
    FileUpload,
    Editor,
  },
  data() {
    return {
      // 服务器地址
      serverUrl:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文档表格数据
      contentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 文档类型动态字典
      contentTypeIdOptions: [],
      // 文档状态字典
      statusIdOptions: [],
      // 创建日期时间范围
      daterangeCreatedDate: [],
      // 版本号动态字典
      versionOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contentId: null,
        contentTypeId: null,
        statusId: null,
        contentName: null,
        createdDate: null,
        createdByUserLogin: null,
        version: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        contentTypeId: [
          { required: true, message: "文档类型不能为空", trigger: "blur" }
        ],
        objectInfo: [
          { required: true, message: "附件不能为空", trigger: "blur" }
        ],
        contentName: [
          { required: true, message: "文档名称不能为空", trigger: "blur" }
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getRemoteOptions("ContentType,contentTypeId,description").then(
      (response) => {
        this.contentTypeIdOptions = response.data;
      }
    );
    this.getDicts("content_status").then((response) => {
      this.statusIdOptions = response.data;
    });
    this.getRemoteOptions("Content,version,version").then((response) => {
      this.versionOptions = response.data;
    });
  },
  methods: {
    // 子组件传过来的数据
    reveiveOriginName(data) {
      this.form.originName = data; //把子组件的文件名称传到form
      this.form.contentName = data; // 显示在文本框的值
      this.originName = data;
    },
    /** 查询文档列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (
        null != this.daterangeCreatedDate &&
        "" != this.daterangeCreatedDate
      ) {
        this.queryParams.params[
          "beginCreatedDate"
        ] = this.daterangeCreatedDate[0];
        this.queryParams.params[
          "endCreatedDate"
        ] = this.daterangeCreatedDate[1];
      }
      listContent(this.queryParams).then((response) => {
        this.contentList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.serverUrl = response.param.serverUrl;
      });
    },
    // 文档类型字典翻译
    contentTypeIdFormat(row, column) {
      return this.selectDictLabel(this.contentTypeIdOptions, row.contentTypeId);
    },
    // 文档状态字典翻译
    statusIdFormat(row, column) {
      return this.selectDictLabel(this.statusIdOptions, row.statusId);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        contentId: null,
        contentTypeId: null,
        ownerContentId: null,
        decoratorContentId: null,
        instanceOfContentId: null,
        objectInfo: null,
        templateDataResourceId: null,
        dataSourceId: null,
        statusId: null,
        privilegeEnumId: null,
        serviceName: null,
        customMethodId: null,
        contentName: null,
        description: null,
        localeString: null,
        mimeTypeId: null,
        characterSetId: null,
        childLeafCount: null,
        childBranchCount: null,
        createdDate: null,
        createdByUserLogin: null,
        lastModifiedDate: null,
        lastModifiedByUserLogin: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null,
        version: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreatedDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.contentId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文档";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const contentId = row.contentId || this.ids;
      getContent(contentId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文档";
      });
    },
    /** 提交审批按钮操作 */
    handleStartWorkflow(row) {
      this.reset();
      const contentId = row.contentId || this.ids;
      this.$confirm(
        '是否确认将文档编号为"' + contentId + '"的数据项提交审批?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return startWorkflowContent(contentId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("提交成功");
        });
    },
    

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.contentId != null) {
            updateContent(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContent(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const contentIds = row.contentId || this.ids;
      this.$confirm(
        '是否确认删除文档编号为"' + contentIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delContent(contentIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有文档数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportContent(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>